package com.hmc.modules.sjfx.controller.tqmtqapsxx;

import com.hmc.common.annotation.LogOperation;
import com.hmc.common.constant.Constant;
import com.hmc.common.page.PageData;
import com.hmc.common.utils.ExcelUtils;
import com.hmc.common.utils.Result;
import com.hmc.common.validator.AssertUtils;
import com.hmc.common.validator.ValidatorUtils;
import com.hmc.common.validator.group.AddGroup;
import com.hmc.common.validator.group.DefaultGroup;
import com.hmc.common.validator.group.UpdateGroup;
import com.hmc.modules.sjfx.dto.tqmtqapsxx.Tqmtqa1DTO;
import com.hmc.modules.sjfx.excel.tqmtqapsxx.Tqmtqa1Excel;
import com.hmc.modules.sjfx.service.tqmtqapsxx.Tqmtqa1Service;

import jakarta.servlet.http.HttpServletResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
* 试批信息
*
* @author hmc wengxiulinluman@qq.com
* @since 3.0 2024-01-12
*/
@AllArgsConstructor
@RestController
@RequestMapping("sjfx/tqmtqa1")
@Tag(name = "试批信息")
public class Tqmtqa1Controller {
    private final Tqmtqa1Service tqmtqa1Service;

    @GetMapping("page")
    @Operation(summary = "分页")
    @Parameters({
        @Parameter(name = Constant.PAGE, description = "当前页码，从1开始", required = true),
        @Parameter(name = Constant.LIMIT, description = "每页显示记录数", required = true),
        @Parameter(name = Constant.ORDER_FIELD, description = "排序字段"),
        @Parameter(name = Constant.ORDER, description = "排序方式，可选值(asc、desc)")
    })
    @RequiresPermissions("sjfx:tqmtqa1:page")
    public Result<PageData<Tqmtqa1DTO>> page(@Parameter(hidden = true) @RequestParam Map<String, Object> params){
        PageData<Tqmtqa1DTO> page = tqmtqa1Service.page(params);

        return new Result<PageData<Tqmtqa1DTO>>().ok(page);
    }

    @GetMapping("{id}")
    @Operation(summary = "信息")
    @RequiresPermissions("sjfx:tqmtqa1:info")
    public Result<Tqmtqa1DTO> get(@PathVariable("id") Long id){
        Tqmtqa1DTO data = tqmtqa1Service.get(id);

        return new Result<Tqmtqa1DTO>().ok(data);
    }

    @PostMapping
    @Operation(summary = "保存")
    @LogOperation("保存")
    @RequiresPermissions("sjfx:tqmtqa1:save")
    public Result save(@RequestBody Tqmtqa1DTO dto){
        //效验数据
        ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);

        tqmtqa1Service.save(dto);

        return new Result();
    }

    @PutMapping
    @Operation(summary = "修改")
    @LogOperation("修改")
    @RequiresPermissions("sjfx:tqmtqa1:update")
    public Result update(@RequestBody Tqmtqa1DTO dto){
        //效验数据
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);

        tqmtqa1Service.update(dto);

        return new Result();
    }

    @DeleteMapping
    @Operation(summary = "删除")
    @LogOperation("删除")
    @RequiresPermissions("sjfx:tqmtqa1:delete")
    public Result delete(@RequestBody Long[] ids){
        //效验数据
        AssertUtils.isArrayEmpty(ids, "id");

        tqmtqa1Service.delete(ids);

        return new Result();
    }

    @GetMapping("export")
    @Operation(summary = "导出")
    @LogOperation("导出")
    @RequiresPermissions("sjfx:tqmtqa1:export")
    public void export(@Parameter(hidden = true) @RequestParam Map<String, Object> params, HttpServletResponse response) throws Exception {
        List<Tqmtqa1DTO> list = tqmtqa1Service.list(params);

        ExcelUtils.exportExcelToTarget(response, null, "试批信息", list, Tqmtqa1Excel.class);
    }

}